﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.ServiceModel;
using System.Collections.Generic;
using CMS.Model;
using System.Collections;
using System.Linq;
using System.Text;
using CMS.Logging;



namespace CMSWCSFSolution.CMS.Views
{
    public partial class SaveCourse : BasePage, ISaveCourseView
    {
        private int _courseID;
        public int[] StudentID;
        #region Ptotected Method
        private SaveCoursePresenter _presenter;
        [CreateNew]
        public SaveCoursePresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.Params["courseid"] != null)
                {
                    _courseID = Convert.ToInt16(Request["courseid"]);
                }
                else
                {
                    _courseID = -1;
                }


                if (!this.IsPostBack)
                {
                    if (_courseID < 0)
                    {
                        title.Text = "Create Course";
                    }
                    else
                    {
                        title.Text = "Update Course";
                    }
                    this._presenter.OnViewInitialized();
                }
                this._presenter.OnViewLoaded();
            }
            catch (Exception ee)
            {
                Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.Highest);
                ShowMessage(3, "3");
            }
        }
        /// <summary>
        /// Handles the Click event of the add control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void add_Click(object sender, EventArgs e)
        {

            try
            {
                if (this.StudentListBox != null && this.StudentListBox.SelectedItem != null)
                {
                    this.SelectedListBox.Items.Add(this.StudentListBox.SelectedItem);
                    this.StudentListBox.Items.Remove(this.SelectedListBox.SelectedItem);
                    this.SelectedListBox.SelectedItem.Selected = false;

                }
            }
            catch (Exception ee)
            {
                Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.High);
                ShowMessage(3, "3");
            }

        }
        /// <summary>
        /// Handles the Click event of the Remove control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Remove_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.SelectedListBox != null && this.SelectedListBox.SelectedItem != null)
                {
                    this.StudentListBox.Items.Add(this.SelectedListBox.SelectedItem);
                    this.SelectedListBox.Items.Remove(this.StudentListBox.SelectedItem);
                    this.StudentListBox.SelectedItem.Selected = false;
                }
            }
            catch (Exception ee)
            {
                Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.High);
                ShowMessage(3, "3");
            }
        }
        /// <summary>
        /// Handles the Click event of the Save control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Save_Click(object sender, EventArgs e)
        {

            try
            {
                StudentID = new int[SelectedListBox.Items.Count];

                for (int i = 0; i < SelectedListBox.Items.Count; i++)
                {
                    int id = int.Parse(SelectedListBox.Items[i].Value);
                    StudentID[i] = id;
                }
                if (_courseID < 0)
                {
                    _presenter.SavePage(_courseID);

                }

                else
                {
                    _presenter.DeleteAllStudentsByCourseId(_courseID);
                    _presenter.SavePage(_courseID);
                }

                ShowMessage(1, "Save");
            }
            catch (Exception ex)
            {
                ShowMessage(3, "error");

            }
        }
        #endregion

        #region Public Property
        /// <summary>
        /// Sets the levels.
        /// </summary>
        /// <value>
        /// The levels.
        /// </value>
        public List<Level> Levels
        {
            set
            {
                this.CourseLevelDropDownList.DataSource = value;
                this.CourseLevelDropDownList.DataTextField = "LevelName";
                this.CourseLevelDropDownList.DataValueField = "LevelId";
                this.CourseLevelDropDownList.DataBind();
                this.CourseLevelDropDownList.Items.Insert(0, "Please Select");
                this.CourseLevelDropDownList.SelectedIndex = 0;
            }
        }
        /// <summary>
        /// Sets the teachers.
        /// </summary>
        /// <value>
        /// The teachers.
        /// </value>
        public List<Teacher> Teachers
        {
            set
            {
                this.TeacherDropDownList.DataSource = value;
                this.TeacherDropDownList.DataTextField = "TeacherName";
                this.TeacherDropDownList.DataValueField = "TeacherId";
                this.TeacherDropDownList.DataBind();
                this.TeacherDropDownList.Items.Insert(0, "Please Select");
                this.TeacherDropDownList.SelectedIndex = 0;
            }
        }
        /// <summary>
        /// Gets the course id.
        /// </summary>
        public int CourseId
        {
            get
            {
                return _courseID;
            }
        }
        /// <summary>
        /// Sets the courses.
        /// </summary>
        /// <value>
        /// The courses.
        /// </value>
        public List<Course> Courses
        {
            set
            {
                this.CourseNameTextBox.Text = value.First().CourseName.Trim();
                this.TeacherDropDownList.SelectedValue = value.First().Teacher.TeacherId.ToString();
                this.CourseLevelDropDownList.SelectedValue = value.First().Level.LevelId.ToString();
                this.StatusRadioButtonList.SelectedValue = value.First().CourseStatus.ToString();
            }
        }
        /// <summary>
        /// Sets the students not in course.
        /// </summary>
        /// <value>
        /// The students not in course.
        /// </value>
        public List<Student> StudentsNotInCourse
        {
            set
            {
                this.StudentListBox.DataSource = value;
                this.StudentListBox.DataTextField = "StudentName";
                this.StudentListBox.DataValueField = "StudentId";
                this.StudentListBox.DataBind();
            }
        }
        /// <summary>
        /// Sets the students in course.
        /// </summary>
        /// <value>
        /// The students in course.
        /// </value>
        public List<Student> StudentsInCourse
        {
            set
            {
                this.SelectedListBox.DataSource = value;
                this.SelectedListBox.DataTextField = "StudentName";
                this.SelectedListBox.DataValueField = "StudentId";
                this.SelectedListBox.DataBind();
            }
        }
        /// <summary>
        /// Gets the index of the level.
        /// </summary>
        /// <value>
        /// The index of the level.
        /// </value>
        public int LevelIndex
        {
            get
            {
                return CourseLevelDropDownList.SelectedIndex;
            }
        }
        /// <summary>
        /// Gets the index of the teacher.
        /// </summary>
        /// <value>
        /// The index of the teacher.
        /// </value>
        public int TeacherIndex
        {
            get
            {
                return TeacherDropDownList.SelectedIndex;
            }
        }
        /// <summary>
        /// Gets the name of the course.
        /// </summary>
        /// <value>
        /// The name of the course.
        /// </value>
        public string CourseName
        {
            get
            {
                return CourseNameTextBox.Text.Trim();
            }
        }
        /// <summary>
        /// Gets the teacher id.
        /// </summary>
        public int TeacherId
        {
            get
            {
                return int.Parse(TeacherDropDownList.SelectedValue.ToString());
            }
        }
        /// <summary>
        /// Gets the level id.
        /// </summary>
        public int LevelId
        {
            get
            {
                return int.Parse(CourseLevelDropDownList.SelectedValue.ToString());
            }
        }
        /// <summary>
        /// Gets a value indicating whether this <see cref="SaveCourse"/> is status.
        /// </summary>
        /// <value>
        ///   <c>true</c> if status; otherwise, <c>false</c>.
        /// </value>
        public bool Status
        {
            get
            {
                return Convert.ToBoolean(StatusRadioButtonList.SelectedValue.ToString());
            }
        }
        /// <summary>
        /// Gets the count.
        /// </summary>
        public int Count
        {
            get
            {
                return SelectedListBox.Items.Count;
            }

        }
        /// <summary>
        /// Gets the student id in selected list.
        /// </summary>
        public int[] StudentIdInSelectedList
        {
            get
            {
                return StudentID;
            }
        }
        #endregion
    }
}


